from utils.pd_show_utils import df_table
from ..base import BaseCrawler
import pandas as pd
import qstock as qs
import os
class Spider_qs_bond_parser(BaseCrawler):
    def __init__(self, name,url, data_process_type,params):
        super().__init__(name,url,data_process_type, params)

    def fetch_one(self,item):
        bond_id = item['转债代码']
        method_name = self.url
        method = getattr(qs, method_name)
        pd_result = method(bond_id)
        return bond_id,pd_result

    def parse(self, raw_data):
        """解析原始数据"""
        if raw_data.isnull().any().any():
            print("存在缺失值 需要单独处理")

        del raw_data['volume']
        del raw_data['name']
        del raw_data['code']
        del raw_data['turnover']
        del raw_data['turnover_rate']
        raw_data = raw_data.reset_index()
        return raw_data
    def append(self,path,bond_id,raw_data):
        
        pre_data = pd.read_csv(os.path.join(path,'csv',bond_id+'_jsl_bond_his.csv'))
        pre_data['date'] = pd.to_datetime(pre_data['date'])
        raw_data['date'] = pd.to_datetime(raw_data['date'])
        full_data = pd.merge(pre_data, raw_data, how='left', on='date')
        del full_data['price']
        full_data.to_csv(os.path.join(path,'csv',bond_id + '_full.csv'),index=False)
        os.remove(os.path.join(path,'csv',bond_id+'_jsl_bond_his.csv'))

